package com.mac.service;

import com.mac.mapper.NewsAndEventsMapper;
import com.mac.pojo.NewsAndEvents;
import com.mac.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
@SuppressWarnings({"all"})
public class NewsAndEventsService implements NewsAndEventsMapper {
    private static final Logger LOGGER = LoggerFactory.getLogger(" NewsAndEventsService.Class");

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public ArrayList<NewsAndEvents> selectAllNewsAndEvents() {
        SqlSession sqlSession = factory.openSession();

        NewsAndEventsMapper newsAndEventsMapper = sqlSession.getMapper(NewsAndEventsMapper.class);

        ArrayList<NewsAndEvents> newsAndEvents;
        try {
            newsAndEvents = newsAndEventsMapper.selectAllNewsAndEvents();
            sqlSession.close();
            LOGGER.info("selectAllNewsAndEvents()-> succeed",newsAndEvents);
            return newsAndEvents;
        } catch (Exception e) {
            sqlSession.close();
            LOGGER.error("selectAllNewsAndEvents() -> failed",e);
            return null;
        }
    }

    @Override
    public NewsAndEvents selectOneByeventId(int eventId) {
        SqlSession sqlSession = factory.openSession();

        NewsAndEventsMapper newsAndEventsMapper = sqlSession.getMapper(NewsAndEventsMapper.class);
        NewsAndEvents newsAndEvents;
        try {
            newsAndEvents = newsAndEventsMapper.selectOneByeventId(eventId);
            sqlSession.close();
            LOGGER.info("selectOneByeventId()-> succeed",newsAndEvents);
            return newsAndEvents;
        } catch (Exception e) {
            sqlSession.close();
            LOGGER.info("selectOneByeventId()-> failed",e);
            return null;
        }
    }
}
